<?php
/**
 * Created by PhpStorm.
 * User: xcx
 * Date: 2017/6/2
 * Time: 10:17
 */

class Controller_Admin_Order extends Controller_Admin_Abstract
{
    /*
     * 未处理小红薯订单列表
     */
    public function listAction(){
//        $this->openDebug();
        $mobile = FRequest::getString('mobile');
        $nickname = FRequest::getString("nickname");
        $source = FRequest::getString("source");
        $intention = FRequest::getString("intention");
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');
        $page = FRequest::getInt("page");
        $channel_id = FRequest::getInt("channel_id");

        $where = array();

        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-6 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $where['uo.create_time']['gte'] = $start_time.' 00:00:00';
        $where['uo.create_time']['lte'] = $end_time.' 23:59:59';

        if($mobile){
            $where['uo.mobile'] = $mobile;
        }
        if($nickname){
            $where['uo.nickname'] = array('like' => "%{$nickname}%");
        }
        if($source){
            $where['uo.source'] =$source;
        }
        if($channel_id){
            $where['ch.id'] =$channel_id;
        }

        if($intention){
            $where['join_intention'] =$intention;
        }


        $where["uo.status"] = array("eq" => 1);


        $table = new FTable('user_order', 'uo', '');
        $list = $table->fields("uo.*, p.name as pro, c.name as cit, ch.channel_name")
            ->leftJoin("privince", "p", "uo.province=p.code")
            ->leftJoin("city", "c", "c.code=uo.city")
            ->leftJoin('channel', 'ch', 'uo.source=ch.domain')
            ->where($where)
            ->order("id desc")
            ->limit(20)
            ->page($page)
            ->select();
        $pagerInfo = $table->getPagerInfo();
        $this->assign('page_info', $pagerInfo);

        //媒体渠道
        $channelTable = new FTable('channel', '', '');
        $channel_list = $channelTable->where('status=1')->select();

//        var_dump($list);
        $this->assign("channel_list",$channel_list);
        $this->assign("data", $list);
        $this->assign("start_time", $start_time);
        $this->assign("end_time", $end_time);
        $this->display('order/order-list');
    }


    /*
     * 已处理小红薯订单列表
     */
    public function listDAction(){
//        $this->openDebug();
        $mobile = FRequest::getString('mobile');
        $nickname = FRequest::getString("nickname");
        $source = FRequest::getString("source");
        $intention = FRequest::getString("intention");
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');
        $page = FRequest::getInt("page");
        $c_uid = FRequest::getInt("c_uid");
        $s_uid = FRequest::getInt("s_uid");

        $where = array();

        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-6 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $where['uo.create_time']['gte'] = $start_time.' 00:00:00';
        $where['uo.create_time']['lte'] = $end_time.' 23:59:59';

        if($mobile){
            $where['mobile'] = $mobile;
        }
        if($nickname){
            $where['nickname'] = array('like' => "%{$nickname}%");
        }
        if($source){
            $where['source'] =$source;
        }
        if($intention){
            $where['join_intention'] =$intention;
        }
        if($s_uid){
            $where['s_uid'] = $s_uid;
        }
        if($c_uid){
            $where['c_uid'] = $c_uid;
        }

        $where["uo.status"] = array("eq" => 2);


        $table = new FTable('user_order', 'uo', '');
        $list = $table->fields("uo.*, p.name as pro, c.name as cit, ch.channel_name")
            ->leftJoin("privince", "p", "uo.province=p.code")
            ->leftJoin("city", "c", "c.code=uo.city")
            ->leftJoin('channel', 'ch', 'uo.source=ch.domain')
            ->where($where)
            ->order("id desc")
            ->limit(20)
            ->page($page)
            ->select();
        $pagerInfo = $table->getPagerInfo();
        $this->assign('page_info', $pagerInfo);

//        var_dump($list);
        $this->assign("data", $list);
        $this->display('order/order-list');
    }

    public function listDelAction(){
//        $this->openDebug();
        $mobile = FRequest::getString('mobile');
        $nickname = FRequest::getString("nickname");
        $source = FRequest::getString("source");
        $intention = FRequest::getString("intention");
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');
        $page = FRequest::getInt("page");
        $c_uid = FRequest::getInt("c_uid");
        $s_uid = FRequest::getInt("s_uid");

        $where = array();

        /*if ($start_time < 1) {
            $start_time = date('Y-m-d', time());
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $where['create_time']['gte'] = $start_time.' 00:00:00';
        $where['create_time']['lte'] = $end_time.' 23:59:59';*/

        if($mobile){
            $where['mobile'] = $mobile;
        }
        if($nickname){
            $where['nickname'] = array('like' => "%{$nickname}%");
        }
        if($source){
            $where['source'] =$source;
        }
        if($intention){
            $where['join_intention'] =$intention;
        }
        if($s_uid){
            $where['s_uid'] = $s_uid;
        }
        if($c_uid){
            $where['c_uid'] = $c_uid;
        }

        $where["uo.status"] = array("eq" => 3);


        $table = new FTable('user_order', 'uo', '');
        $list = $table->fields("uo.*, p.name as pro, c.name as cit, ch.channel_name")
            ->leftJoin("privince", "p", "uo.province=p.code")
            ->leftJoin("city", "c", "c.code=uo.city")
            ->leftJoin('channel', 'ch', 'uo.source=ch.domain')
            ->where($where)
            ->order("id desc")
            ->limit(20)
            ->page($page)
            ->select();
        $pagerInfo = $table->getPagerInfo();
        $this->assign('page_info', $pagerInfo);

//        var_dump($list);
        $this->assign("data", $list);
        $this->display('order/order-list-del');
    }

    /*
     * 订单处理
     */
    public function orderModifyAction(){
        global $_F;
        $id = FRequest::getInt("id");
        if($this->isPost()){
            $id = FRequest::getPostInt('id');
            $remarks = FRequest::getPostString("remarks");
            $status = FRequest::getPostInt("status");

            $save = array();
            $save['remarks'] = $remarks;
            $save['status'] = $status;
            $table = new FTable("user_order", "", "");
            $table->update1($save, array("id" => $id));
            $this->success("修改成功", $_F['uri'] . "?id=" . $id);
        }else{
            $table =  new FTable("user_order", "", "");
            $data = $table->fields("*")->where(array("id" => $id))->find();
            $this->assign("data", $data);
        }
        $this->display("order/modify");
    }

    /*
     * 报表导出功能
     */
    public function excelDaoAction(){
        global $_F;

        $where = array();


        if ($_GET['begin_date']) {
            $where['create_time'] = array('gte' => $_GET['begin_date']);
        }

        if ($_GET['end_date']) {
            $where['create_time']['lte'] = $_GET['end_date'];
        }

        if($_GET['nickname']){
            $where['nickname'] = $_GET['nickname'];
        }

        if($_GET['mobile']){
            $where['mobile'] = $_GET['mobile'];
        }

        if($_GET['intention']){
            $where['join_intention'] = $_GET['intention'];
        }




        $reportT = new FTable('user_order', 'uo', '');
        $list = $reportT->fields("uo.*, p.name as pro, c.name as cit")
            ->leftJoin("privince", "p", "uo.province=p.code")
            ->leftJoin("city", "c", "c.code=uo.city")
            ->where($where)
            ->order(array('id' => 'desc'))
            ->select();




        $data = array();
        $data[0] = array("ID", "昵称", "联系电话", "加盟意向", "地址", "来源", "时间", "备注", "状态");

        foreach ($list as $k => $o) {
            $data[$k + 1][0] = $o['id'];
            $data[$k + 1][1] = $o['nickname'];
            $data[$k + 1][2] = $o['mobile'];
            if($o['join_intention'] == 1){
                $data[$k + 1][3] = "单店代理";
            }elseif($o['join_intention'] == 2){
                $data[$k + 1][3] = "区域代理";
            }else{
                $data[$k + 1][3] = "";
            }
            $data[$k + 1][4] = "{$o['pro']}--{$o['cit']}";
            $data[$k + 1][5] = $o['source'];
            $data[$k + 1][6] = $o['create_time'];
            $data[$k + 1][7] = $o['remarks'];
            if($o['status'] == 1){
                $data[$k + 1][8] = "未处理";
            }elseif($o['status'] == 2){
                $data[$k + 1][8] = "已联系";
            }else{
                $data[$k + 1][8] = "";
            }
        }

        $sheetName = "{$_GET['begin_date']}至{$_GET['end_date']}";
        $fileName = iconv("utf-8", "gb2312", "{$sheetName}充值统计导出");
        $xls = new FExcel('UTF-8', false, $sheetName);
        $xls->addArray($data);
        $xls->generateXML($fileName);

        exit();
    }

    public function delAction(){
        global $_F;
        $flag = FRequest::getInt("flag") ? FRequest::getInt("flag") : 0;
        $id = FRequest::getInt("id");
        if ($id) {
            FDB::remove("user_order", array("id" => $id), true);
            $this->success("删除成功!", $_F['refer']);
        } else {
            $this->success("删除失败，id为空！", $_F['refer']);
        }
    }

    public function modifyStatusAction(){
        global $_F;
        $flag = FRequest::getInt("flag") ? FRequest::getInt("flag") : 0;
        $id = FRequest::getInt("id");
        $status = FRequest::getInt("status");
        if ($id) {
            $table = new FTable("user_order", "", "");
            $table->update1(array('status' => $status), array("id" => $id));
            $this->success("操作成功!", $_F['refer']);
        } else {
            $this->success("操作失败，id为空！", $_F['refer']);
        }
    }


}